rm(list = ls())
here::i_am(file.path("code", "16_questionnaires.R"))
library(here)
source(here("code", "config.R"))

df <- read_parquet(
  here("data", "analysis", "questionnaires.parquet")
)

p <- df |>
  summarize(
    across(
      c(
        exposure,
        hardship,
        god,
        manhood,
        duty,
        military,
        injustice,
        patriotism,
        camaraderie
      ),
      mean
    )
  ) |>
  pivot_longer(everything()) |>
  mutate(
    name = factor(
      name,
      levels = c(
        "exposure",
        "hardship",
        "god",
        "manhood",
        "duty",
        "military",
        "injustice",
        "patriotism",
        "camaraderie"
      ), labels = c(
        "Broader mind",
        "Hardship",
        "Religion",
        "Manhood",
        "Civic duty",
        "Training",
        "Injustice",
        "Patriotism",
        "Camaraderie"
      )
    )
  ) |>
  ggplot(aes(x = name, y = value)) +
  my_theme() +
  labs(
    x = "",
    y = "Share of\nquestionnaires"
  ) +
  scale_x_discrete(guide = guide_axis(angle = 45))

p_color <- p + geom_col(width = .75, fill = "#F8766D")
ggsave(file.path(fig_dir, "color", "questionnaires_themes.pdf"), plot = p_color, width = 5, height = 3.4)

p_bw <- p + geom_col(width = .75)
ggsave(file.path(fig_dir, "bw", "questionnaires_themes.pdf"), plot = p_bw, width = 5, height = 3.4)

p <- df |>
  mutate(
    discriminatory_camp = (nco == 0) | (training == 0)
  ) |>
  filter(!is.na(discriminatory_camp)) |>
  group_by(discriminatory_camp) |>
  summarize(
    across(c(exposure, hardship, god, manhood, duty, military, injustice, patriotism, camaraderie), mean)
  ) |>
  pivot_longer(-discriminatory_camp) |>
  pivot_wider(names_from = discriminatory_camp, values_from = value) |>
  mutate(ratio = 100*((`TRUE` / `FALSE`) - 1)) |>
  mutate(
    name = factor(
      name,
      levels = c("exposure", "hardship", "god", "manhood", "duty", "military", "injustice", "patriotism", "camaraderie"),
      labels = c("Broader mind", "Hardship", "Religion", "Manhood", "Civic duty", "Training", "Injustice", "Patriotism", "Camaraderie")
    )
  ) |>
  ggplot(aes(x = name, y = ratio)) +
  my_theme() +
  labs(
    x = "",
    y = "Pct. difference at most\ndiscriminatory camps"
  ) +
  scale_x_discrete(guide = guide_axis(angle = 45))

p_color <- p + geom_col(width = .75, fill = "#F8766D")
ggsave(file.path(fig_dir, "color", "questionnaires_camps.pdf"), plot = p_color, width = 5, height = 3.5)

p_bw <- p + geom_col(width = .75)
ggsave(file.path(fig_dir, "bw", "questionnaires_camps.pdf"), plot = p_bw, width = 5, height = 3.5)

# NB: "vet" is prior military experience
p_color <- feols(
  vote ~ exposure + hardship + god + manhood + duty + military + injustice + patriotism + camaraderie |
    sw0(birth_year + birth_state + vet + state^county),
  data = df
) |>
  coeftable() |>
  filter(
    coefficient != "(Intercept)"
  ) |>
  mutate(
    coefficient = factor(
      coefficient,
      levels = c("exposure", "hardship", "god", "manhood", "duty", "military", "injustice", "patriotism", "camaraderie"),
      labels = c("Broader mind", "Hardship", "Religion", "Manhood", "Civic duty", "Training", "Injustice", "Patriotism", "Camaraderie")
    ),
    fixef = factor(
      fixef,
      levels = c("1", "birth_year + birth_state + vet + state^county"),
      labels = c("Baseline", "Controls")
    )
  ) |>
  ggplot(aes(x = Estimate, xmin = Estimate - 1.96*`Std. Error`, xmax = Estimate + 1.96*`Std. Error`,
             y = fct_rev(coefficient), shape = fixef)) +
  geom_vline(xintercept = 0, linetype = "dashed", alpha = .7) +
  my_theme() +
  labs(
    x = "Coefficient on voting",
    y = "",
    color = "",
    shape = ""
  ) +
  geom_point(aes(color = fixef), size = 2, position = position_dodge2(.7, reverse = TRUE)) +
  geom_linerange(aes(color = fixef), position = position_dodge2(.7, reverse = TRUE))
ggsave(file.path(fig_dir, "color", "questionnaires_vote.pdf"), plot = p_color, width = 5, height = 4)

p_bw <- p_color + 
  scale_color_manual(values = c("Baseline" = "black", "Controls" = "#555"))
ggsave(file.path(fig_dir, "bw", "questionnaires_vote.pdf"), plot = p_bw, width = 5, height = 4)

p <- df |>
  filter(!is.na(diff_bw_q4)) |>
  group_by(diff_bw_q4) |>
  summarize(
    across(c(exposure, hardship, god, manhood, duty, military, injustice, patriotism, camaraderie), mean)
  ) |>
  pivot_longer(-diff_bw_q4) |>
  pivot_wider(names_from = diff_bw_q4, values_from = value) |>
  mutate(ratio = 100*((`1` / `0`) - 1)) |>
  mutate(name = factor(
    name,
    levels = c(
      "exposure",
      "hardship",
      "god",
      "manhood",
      "duty",
      "military",
      "injustice",
      "patriotism",
      "camaraderie"
    ),
    labels = c(
      "Broader mind",
      "Hardship",
      "Religion",
      "Manhood",
      "Civic duty",
      "Training",
      "Injustice",
      "Patriotism",
      "Camaraderie"
    )
  )) |> 
  ggplot(aes(x = name, y = ratio)) +
  my_theme() +
  labs(
    x = "",
    y = "Pct. difference at most\ndiscriminatory boards"
  ) +
  scale_x_discrete(guide = guide_axis(angle = 45))

p_color <- p + geom_col(width = .75, fill = "#F8766D")
ggsave(file.path(fig_dir, "color", "questionnaires_boards.pdf"), plot = p_color, width = 5, height = 3.5)

p_bw <- p + geom_col(width = .75)
ggsave(file.path(fig_dir, "bw", "questionnaires_boards.pdf"), plot = p_bw, width = 5, height = 3.5)
